import datetime
from mdscpro.settings import SECRET_KEY
import jwt


def make_token(user_id):
    '''
    utcnow()世界标准时间
    '''
    payload = {
        'user_id': user_id,
        'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1)
    }
    #序列化成字符串，返回格式是bytes类型，需要进行decode()
    token=jwt.encode(payload=payload,key=SECRET_KEY).decode()
    return token

def check_token(token):
    #反序列化信息
    try:
        user_info=jwt.decode(token,key=SECRET_KEY)
    except jwt.exceptions.ExpiredSignatureError:
        return None
    else:
        return user_info

if __name__ == '__main__':
    token=make_token('1')
    import time
    time.sleep(11)
    print(check_token(token))
